<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="实验编号" prop="courseNo">
        <el-input
          v-model="queryParams.courseNo"
          placeholder="请输入实验编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入实验名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验类别" prop="courseCategory">
        <el-select v-model="queryParams.courseCategory" placeholder="请选择实验类别" clearable>
          <el-option
            v-for="dict in dict.type.biz_course_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实验所属学科" prop="subjectName">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入实验所属学科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验者类别" prop="experimenterCategory">
        <el-select v-model="queryParams.experimenterCategory" placeholder="请选择实验者类别" clearable>
          <el-option
            v-for="dict in dict.type.biz_experimenter_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否需要指导老师" prop="isNeedAdviser">
        <el-select v-model="queryParams.isNeedAdviser" placeholder="请选择是否需要指导老师" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实验所属课程名称" prop="subjectCourseName">
        <el-input
          v-model="queryParams.subjectCourseName"
          placeholder="请输入实验所属课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:course:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:course:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:course:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:course:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="实验编号" align="center" width="140" fixed prop="courseNo"/>
      <el-table-column label="实验名称" align="center" width="140" prop="courseName"/>
      <el-table-column label="实验类别" align="center" prop="courseCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_course_category" :value="scope.row.courseCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="实验所属学科" width="100"  align="center" prop="subjectName"/>
      <el-table-column label="实验要求" width="100"  align="center" prop="experimentRequirements"/>
      <el-table-column label="实验者类别" width="100"  align="center" prop="experimenterCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_experimenter_category" :value="scope.row.experimenterCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="是否需要指导老师" width="150" align="center" prop="isNeedAdviser">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isNeedAdviser"/>
        </template>
      </el-table-column>
      <el-table-column label="实验人数上限" width="150" align="center" prop="maxExperimenters"/>
      <el-table-column label="实验学时数" width="100"  align="center" prop="experimentHours"/>
      <el-table-column label="实验室" width="120"  align="center" prop="labName"/>
      <el-table-column label="所属课程名称" width="140"  align="center" prop="subjectCourseName"/>
      <el-table-column label="实验开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实验结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="130" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:course:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:course:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实验课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="实验编号" prop="courseNo">
          <el-input placeholder="请输入实验编号" v-model="form.courseNo" class="input-with-select">
            <el-button slot="append" @click="generateCourseNo">生成</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="实验名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入实验名称"/>
        </el-form-item>
        <el-form-item label="实验类别" prop="courseCategory">
          <el-select v-model="form.courseCategory" placeholder="请选择实验类别">
            <el-option
              v-for="dict in dict.type.biz_course_category"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验所属学科" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入实验所属学科"/>
        </el-form-item>
        <el-form-item label="实验要求" prop="experimentRequirements">
          <el-input v-model="form.experimentRequirements" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="实验者类别" prop="experimenterCategory">
          <el-select v-model="form.experimenterCategory" placeholder="请选择实验者类别">
            <el-option
              v-for="dict in dict.type.biz_experimenter_category"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否需要指导老师" prop="isNeedAdviser">
          <el-select v-model="form.isNeedAdviser" placeholder="请选择是否需要指导老师">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验人数上限" prop="maxExperimenters">
          <el-input v-model="form.maxExperimenters" placeholder="请输入实验人数上限"/>
        </el-form-item>
        <el-form-item label="实验学时数" prop="experimentHours">
          <el-input v-model="form.experimentHours" placeholder="请输入实验学时数"/>
        </el-form-item>
        <el-form-item label="实验室" prop="labId">
          <el-select v-model="form.labId" placeholder="请选择所属实验室">
            <el-option v-for="item in labList" :label="item.labName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验所属课程名称" prop="subjectCourseName">
          <el-input v-model="form.subjectCourseName" placeholder="请输入实验所属课程名称"/>
        </el-form-item>
        <el-form-item label="实验开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            placeholder="请选择实验开始时间"
            default-time="12:00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实验结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            placeholder="请选择实验结束时间"
            default-time="12:00:00">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCourse, getCourse, delCourse, addCourse, updateCourse} from "@/api/system/course";
import {listLab} from "@/api/system/lab";

export default {
  name: "Course",
  dicts: ['sys_yes_no', 'biz_course_category', 'biz_experimenter_category'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验课程表格数据
      courseList: [],
      labList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: null,
        courseName: null,
        courseCategory: null,
        subjectName: null,
        experimentRequirements: null,
        experimenterCategory: null,
        isNeedAdviser: null,
        subjectCourseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseNo: [
          {required: true, message: "实验编号不能为空", trigger: "blur"}
        ],
        courseName: [
          {required: true, message: "实验名称不能为空", trigger: "blur"}
        ],
        courseCategory: [
          {required: true, message: "实验类别不能为空", trigger: "change"}
        ],
        subjectName: [
          {required: true, message: "实验所属学科不能为空", trigger: "blur"}
        ],
        experimenterCategory: [
          {required: true, message: "实验者类别不能为空", trigger: "change"}
        ],
        isNeedAdviser: [
          {required: true, message: "是否需要指导老师不能为空", trigger: "change"}
        ],
        maxExperimenters: [
          {required: true, message: "实验人数上限不能为空", trigger: "blur"}
        ],
        experimentHours: [
          {required: true, message: "实验学时数不能为空", trigger: "blur"}
        ],
        labId: [
          {required: true, message: "实验室id不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "实验开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "实验结束时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getLabList();
  },
  methods: {
    getLabList() {
      listLab().then(response => {
        this.labList = response.rows;
      });
    },
    /** 查询实验课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseNo: null,
        courseName: null,
        courseCategory: null,
        subjectName: null,
        experimentRequirements: null,
        experimenterCategory: null,
        isNeedAdviser: null,
        maxExperimenters: null,
        experimentHours: null,
        labId: null,
        subjectCourseName: null,
        startTime: null,
        endTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    generateCourseNo() {
      // 生产实验课程编号
      this.form.courseNo = "C" + new Date().getTime();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实验课程编号为"' + ids + '"的数据项？').then(function () {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
